package com.yanqu.road.dao.impl.activity.carptrip;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.carptrip.UserCarpTripEvent;
import com.yanqu.road.entity.activity.carptrip.data.UserCarpTripData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserCarpTripDataDaoImpl extends TempDataObjectDao<UserCarpTripData> {

    public static String getUserCarpTripDataSql = "select * from `t_u_carp_trip_data` where `activityId`=? and `userId`=?";
    public UserCarpTripData getUserCarpTripData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCarpTripDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserCarpTripDataSql = "insert into `t_u_carp_trip_data`(activityId,userId,exp,score,ability,power,lastRecoverPowerTime,drawTimes,cellNum,event,gameData,topCellNum,topCellList,dragonIdList,drawUpLv,drawProcess,drawExp,lastReadTime," +
            "preHandleList,launchTimes,drawRewardMap,drawCountMap,clientSave) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserCarpTripData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExp().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getGameData()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTopCellNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTopCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDragonIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawUpLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawProcess()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawExp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReadTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPreHandleList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLaunchTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawRewardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawCountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getClientSave()));
        if (getDb().executeNoneQuery(insertUserCarpTripDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserCarpTripDataSql = "update `t_u_carp_trip_data` set exp=?,score=?,ability=?,power=?,lastRecoverPowerTime=?,drawTimes=?,cellNum=?,event=?,gameData=?,topCellNum=?,topCellList=?,dragonIdList=?,drawUpLv=?,drawProcess=?,drawExp=?,lastReadTime=?" +
            ",preHandleList=?,launchTimes=?,drawRewardMap=?,drawCountMap=?,clientSave=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserCarpTripData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExp().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getGameData()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTopCellNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTopCellList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDragonIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawUpLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawProcess()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawExp()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReadTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPreHandleList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLaunchTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawRewardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawCountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getClientSave()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserCarpTripDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserCarpTripData parseData(ResultSet rs) throws SQLException {
        UserCarpTripData data = new UserCarpTripData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setExp(new BigInteger(rs.getString("exp")));
        data.setScore(rs.getInt("score"));
        data.setAbility(new BigInteger(rs.getString("ability")));
        data.setPower(rs.getInt("power"));
        data.setLastRecoverPowerTime(rs.getLong("lastRecoverPowerTime"));
        data.setDrawTimes(rs.getInt("drawTimes"));
        data.setCellNum(rs.getInt("cellNum"));
        data.setEvent(JSON.parseObject(rs.getString("event"), UserCarpTripEvent.class));
        data.setGameData(rs.getString("gameData"));
        data.setTopCellNum(rs.getInt("topCellNum"));
        data.setTopCellList(JSON.parseObject(rs.getString("topCellList"), new TypeReference<List<Integer>>() {}));
        data.setDragonIdList(JSON.parseObject(rs.getString("dragonIdList"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Boolean>>() {}));
        data.setDrawUpLv(rs.getInt("drawUpLv"));
        data.setDrawProcess(rs.getInt("drawProcess"));
        data.setDrawExp(rs.getInt("drawExp"));
        data.setLastReadTime(rs.getLong("lastReadTime"));
        data.setPreHandleList(StringUtils.stringToIntegerList(rs.getString("preHandleList"), ","));
        data.setLaunchTimes(rs.getInt("launchTimes"));
        String str = rs.getString("drawRewardMap");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setDrawRewardMap(JSON.parseObject(str, new TypeReference<Map<Integer, String>>() {}));
        }
        str = rs.getString("drawCountMap");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setDrawCountMap(JSON.parseObject(str, new TypeReference<Map<Integer, Integer>>() {}));
        }
        data.setClientSave(rs.getInt("clientSave"));
        return data;
    }
}
